package version2024.medium;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
 * @author sunhl
 * @Description: HJ36 字符串加密  字符, ASCII, 集合
 * @Date: created in 2024/12/29 星期日 18:02
 * @Modified By:
 */
public class HJ36_StringEncryption {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            String s = in.next();
            Map<Character, Character> map = getKeyMap(s);

            String t = in.next();
            System.out.println(decodeStr(map, t));

        }
    }

    // 获取加密的基础表, map保存
    public static Map<Character, Character> getKeyMap(String s) {
        ArrayList<Character> list = new ArrayList<>();
        for (int i = 0; i < s.length(); i++) {
            if (!list.contains(s.charAt(i))) {
                list.add(s.charAt(i));
            }
        }

        // 大写字母‌：ASCII码值从65到90
        // 小写字母: ASCII码值从97到122
        // 题明确说只有小写
        for (int i = 97; i <= 122; i++) {
            char c = (char) i;
            if (!list.contains(c)) {
                list.add(c);
            }
        }

        Map<Character, Character> map = new HashMap<>();
        int begin = 97;
        for (int i = 0; i < list.size(); i++) {
            map.put((char) (begin + i), list.get(i));
        }

        return map;
    }

    // 根据加密表对t进行加密
    public static String decodeStr(Map<Character, Character> map, String t) {
        StringBuilder result = new StringBuilder();

        for (int i = 0; i < t.length(); i++) {
            char c = t.charAt(i);
            Character dc = map.get(c);
            // 这里考虑了一下大小写, 实际上t只会由小写字母构成
            if (c >= 97 && c <= 122) {
                result.append(dc);
            } else {
                result.append(Character.toUpperCase(c));
            }
        }

        return result.toString();
    }
}
